package com.tianyang.yukuaifuzs.business.main.adapter;

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;

import com.tianyang.yukuaifuzs.business.main.fragment.HomePageFragment;
import com.tianyang.yukuaifuzs.business.main.fragment.MyServiceFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 主界面viewpager adapter
 * ViewPager清除已加载Fragment，重新创建新的Fragment的方法
 */
public class MainAdapter extends FragmentPagerAdapter {

    private FragmentManager mFragmentManager;
    private FragmentTransaction mCurTransaction;
    private List<Fragment> mFragments = new ArrayList<>();

    public MainAdapter(FragmentManager fm, String identity) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        this.mFragmentManager = fm;
        mFragments.clear();
        mFragments.add(new HomePageFragment());
        mFragments.add(new MyServiceFragment());
//        switch (identity) {
//            case MainConfig.IDENTITY_MERCHANT:
//                mFragments.add(new MyFragment());
//                break;
//            case MainConfig.IDENTITY_AGENT:
//                mFragments.add(new HomePageFragment());
//                mFragments.add(new MyServiceFragment());
//                break;
//            case MainConfig.IDENTITY_TOP:
//                mFragments.add(new HomePageFragment());
//                mFragments.add(new MyServiceFragment());
//                mFragments.add(new MyFragment());
//                break;
//        }
    }

    @Override
    public Fragment getItem(int i) {
        return mFragments.get(i);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return position + "";
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    /**
     * 清除缓存fragment
     * @param container ViewPager
     */
    public void clear(ViewGroup container){
        if (this.mCurTransaction == null) {
            this.mCurTransaction = this.mFragmentManager.beginTransaction();
        }

        for(int i=0;i<mFragments.size();i++){
            long itemId = this.getItemId(i);
            String name = makeFragmentName(container.getId(), itemId);
            Fragment fragment = this.mFragmentManager.findFragmentByTag(name);
            if(fragment != null){//根据对应的ID，找到fragment，删除
                mCurTransaction.remove(fragment);
            }
        }
        mCurTransaction.commitNowAllowingStateLoss();
    }

    /**
     * 等同于FragmentPagerAdapter的makeFragmentName方法，
     * 由于父类的该方法是私有的，所以在此重新定义
     * @param viewId
     * @param id
     * @return
     */
    private static String makeFragmentName(int viewId, long id) {
        return "android:switcher:" + viewId + ":" + id;
    }
}
